Imports GeoFramework
Imports GeoFramework.IO

Public Class MonitoringDetectingDevicesForm

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Bind into all events for device detection
        AddHandler Devices.DeviceDetectionStarted, AddressOf Devices_DeviceDetectionStarted
        AddHandler Devices.DeviceDetectionAttempted, AddressOf Devices_DeviceDetectionAttempted
        AddHandler Devices.DeviceDetected, AddressOf Devices_DeviceDetected
        AddHandler Devices.DeviceDetectionCanceled, AddressOf Devices_DeviceDetectionCancelled
        AddHandler Devices.DeviceDetectionCompleted, AddressOf Devices_DeviceDetectionCompleted
        AddHandler Devices.DeviceDetectionExceptionOccurred, AddressOf Devices_ExceptionOccurred

        ' Enable/disable the Detect and Cancel buttons depending on whether
        ' detection is already in progress
        If Devices.IsDetectionInProgress Then
            DetectButton.Enabled = False
            CancelDetectionButton.Enabled = True
        Else
            DetectButton.Enabled = True
            CancelDetectionButton.Enabled = False
        End If
    End Sub

#Region " Starting and Canceling Device Detection "

    Private Sub DetectButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles DetectButton.Click
        ' Start device detection
        Devices.Detect()

        ' Disable Detect, enable Cancel
        DetectButton.Enabled = False
        CancelDetectionButton.Enabled = True
    End Sub

    Private Sub CancelDetectionButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles CancelDetectionButton.Click
        ' Cancel detection
        Devices.CancelDetection()

        ' Disable Cancel, enable Detect
        DetectButton.Enabled = False
        CancelDetectionButton.Enabled = True
    End Sub

#End Region

#Region " Device Detection Events "

    Private Sub Devices_DeviceDetectionStarted(ByVal sender As Object, ByVal e As EventArgs)
        ' Show that detection has started
        MessageListBox.Items.Add("Starting device detection.")
    End Sub

    Private Sub Devices_DeviceDetectionAttempted(ByVal sender As Object, ByVal e As DeviceEventArgs)
        ' Show that detection has started for the device
        MessageListBox.Items.Add("Attempting detection on " + e.Device.ToString())
    End Sub

    Private Sub Devices_DeviceDetected(ByVal sender As Object, ByVal e As DeviceEventArgs)
        ' Show that the device was detected
        MessageListBox.Items.Add("Found GPS Device: " + e.Device.ToString())
    End Sub

    Private Sub Devices_DeviceDetectionCancelled(ByVal sender As Object, ByVal e As DeviceEventArgs)
        ' Show that the device was cancelled
        MessageListBox.Items.Add("Cancelled detection on " + e.Device.ToString())
    End Sub

    Private Sub Devices_DeviceDetectionCompleted(ByVal sender As Object, ByVal e As EventArgs)
        ' Show that detection has started
        MessageListBox.Items.Add("Device detection completed.")

        ' Re-enable the Detect button
        DetectButton.Enabled = True
        CancelDetectionButton.Enabled = False
    End Sub

    Private Sub Devices_ExceptionOccurred(ByVal sender As Object, ByVal e As ExceptionEventArgs)
        ' Show that detection has started
        MessageListBox.Items.Add("ERROR: " + e.Exception.ToString())
    End Sub

#End Region

End Class